package com.zrrd.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.zrrd.reggie.bean.Category;
import com.zrrd.reggie.bean.Dish;
import com.zrrd.reggie.bean.DishDto;
import com.zrrd.reggie.bean.DishFlavor;
import com.zrrd.reggie.common.R;
import com.zrrd.reggie.service.CategoryService;
import com.zrrd.reggie.service.DishService;
import com.zrrd.reggie.service.DishFlavorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.Signature;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private DishFlavorService dishFlavorService;
    private String name;


    //分页
    @GetMapping("/page")
    //分页展示列表信息
    public R<Page> page(Integer page,Integer pageSize){
        Page<Dish> pageInfo = new Page<>(page,pageSize);
        QueryWrapper<Dish> wrapper = new QueryWrapper<>();

        if (name != null && !name.equals("")){
            wrapper.like("name",name);
        }

        wrapper.orderByDesc("sort");
        dishService.page(pageInfo,wrapper);

        //新建一个dto的分页对象
        Page<DishDto> pageDto = new Page<>();
        //使用工具类 进行对象拷贝
        BeanUtils.copyProperties(pageInfo,pageDto,"records");
        //获取菜品的集合信息
        List<Dish> dishList = pageInfo.getRecords();
        //创建一个dto集合
        List<DishDto> dtoList = new ArrayList<>();
        //遍历菜品集合
        for (Dish dish : dishList){
            //创建dto对象
            DishDto dto = new DishDto();
            //使用对象拷贝
            BeanUtils.copyProperties(dish,dto);
            //根据分类名称查询分类信息
            Category category = categoryService.getById(dish.getCategoryId());
            //给dto添加进dto的集合
            dtoList.add(dto);
        }
        //给dto分页对象  重新设置集合数据
        pageDto.setRecords(dtoList);
        //返回结果
        return R.success(pageDto);
    }

    //添加菜品
    @PostMapping
    public R<String> save(@RequestBody DishDto dto){
        log.info("收到添加菜品的请求==>{}",dto);
        //调用业务层执行添加
        dishService.saveDish(dto);
        return R.success("添加成功");

    }

    //根据菜品id获取菜品信息（包含口味）
    @GetMapping("/{id}")
    public R<DishDto> getById(@PathVariable("id") Long id){
        //获取菜品信息
        Dish dish = dishService.getById(id);
        //创建菜品dto对象
        DishDto dto = new DishDto();
        //复制函数（将dish的属性复制到dto）
        BeanUtils.copyProperties(dish,dto);


        //根据菜品id查询口味表
        QueryWrapper<DishFlavor> wrapper = new QueryWrapper<>();
        wrapper.eq("dish_id",id);
        List<DishFlavor> flavorList = dishFlavorService.list(wrapper);
        //将口味的集合设置给对象
        dto.setFlavors(flavorList);
        //返回dto对象
        return R.success(dto);
    }

    //修改菜品
    @PutMapping
    public R<String> update(@RequestBody DishDto dto){
        dishService.updateDish(dto);
        return R.success("修改成功");
    }

    //删除菜品
    @DeleteMapping
    public R<String> delete(Long[] ids){
        dishService.deleteDish(ids);
        //返回
        return R.success("删除成功");
    }


    //修改状态
    @PostMapping("/status/{status}")
    public R<String> status(@PathVariable("status") Integer status,Long[] ids){
        //根据id批量查询
        List<Dish> dishList = dishService.listByIds(Arrays.asList(ids));
        //遍历集合
        for (Dish dish : dishList){
            //给菜品设置状态
            dish.setStatus(status);
        }
        //批量修改
        dishService.updateBatchById(dishList);
        return R.success("修改成功");
    }


    //根据分类id查询菜品列表
    @GetMapping("/list")
    public R<List<DishDto>> list(Long categoryId){
        QueryWrapper<Dish> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id",categoryId);

        //菜品状态必须是1（只查询可售的菜品)
        wrapper.eq("status",1);

        List<Dish> dishList = dishService.list(wrapper);
        //创建最终返回的dto的集合
        List<DishDto> dtoList = new ArrayList<>();

        for (Dish dish : dishList) {
            DishDto dto = new DishDto();
            BeanUtils.copyProperties(dish,dto);
            QueryWrapper<DishFlavor> flavorQueryWrapper = new QueryWrapper<>();
            flavorQueryWrapper.eq("dish_id",dish.getId());
            List<DishFlavor> flavorList = dishFlavorService.list(flavorQueryWrapper);
            //将口味集合设置给dto
            dto.setFlavors(flavorList);
            //将dto添加进dto的集合
            dtoList.add(dto);
        }
        return R.success(dtoList);
    }


}
